<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Vue源码开发-Render函数03 | 北城韩雨</title><meta name="keywords" content="Vue"><meta name="author" content="北城韩雨"><meta name="copyright" content="北城韩雨"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta name="description" content="Vue">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue源码开发-Render函数03">
<meta property="og:url" content="http://example.com/2021/01/03/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8BRender%E5%87%BD%E6%95%B003/index.html">
<meta property="og:site_name" content="北城韩雨">
<meta property="og:description" content="Vue">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530659&t=bbcfe1e86f393eecbb0722db1b92e475">
<meta property="article:published_time" content="2021-01-02T16:00:00.000Z">
<meta property="article:modified_time" content="2021-01-02T16:00:00.000Z">
<meta property="article:author" content="北城韩雨">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530659&t=bbcfe1e86f393eecbb0722db1b92e475"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2021/01/03/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8BRender%E5%87%BD%E6%95%B003/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'mediumZoom',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Vue源码开发-Render函数03',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-01-03 00:00:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/custom.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/coin.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/liuxing.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zhheo/JS-Heo@main/bb/showbb_in_index.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" media="print" onload="this.media='screen'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-butterfly-tag-plugins-plus/lib/tag_plugins.min.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/my.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">北城韩雨</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Vue源码开发-Render函数03</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-01-02T16:00:00.000Z" title="发表于 2021-01-03 00:00:00">2021-01-03</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-01-02T16:00:00.000Z" title="更新于 2021-01-03 00:00:00">2021-01-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%BA%90%E7%A0%81/">源码</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>11分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Vue源码开发-Render函数03"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Vue源码开发-模版解析"><a href="#Vue源码开发-模版解析" class="headerlink" title="Vue源码开发-模版解析"></a>Vue源码开发-模版解析</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 前面 我们已经 把我们的 HTML 解析成我们的 AST 语法树了</span><br><span class="line"><span class="number">2.</span> 接下来我们 把我们生成好的 AST语法树 编译成我们的 render函数</span><br></pre></td></tr></table></figure>
<h2 id="如何变成我们的render函数"><a href="#如何变成我们的render函数" class="headerlink" title="如何变成我们的render函数"></a>如何变成我们的render函数</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 核心思路就是字符串拼接 </span><br><span class="line"><span class="number">2.</span> 因为上面我们已经把我们的 HTML结构 解析成了AST语法树</span><br><span class="line"><span class="number">3.</span> 我们会根据AST语法树中的东西 做函数的字符串拼接</span><br><span class="line"><span class="number">4.</span> 最后render函数会把我们的创建好的变成虚拟DOM</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span> <span class="attr">a</span>=<span class="string">&quot;1&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">span</span> <span class="attr">style</span>=<span class="string">&quot;color:red;&quot;</span>&gt;</span></span><br><span class="line">        &#123;&#123;name&#125;</span><br><span class="line">        <span class="tag">&lt;<span class="name">a</span>&gt;</span> hello<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">    &#125;<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span> </span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//1.1 我们就要把上面的东西 生成如下的代码</span></span><br><span class="line"><span class="comment">//_c 函数 就是创建DOM的 第一个参数 是你要创建的 元素名称</span></span><br><span class="line"><span class="comment">// 第二个参数 是你的属性集合</span></span><br><span class="line"><span class="comment">// 第二个参数以后 就是 你的孩子参数</span></span><br><span class="line"><span class="comment">// 如果你的孩子还是是个元素 那么就接着用_c函数继续创建</span></span><br><span class="line"><span class="comment">// _s函数 用来解析你的&#123;&#123;&#125;&#125;括号里面的东西</span></span><br><span class="line"><span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> _c(</span><br><span class="line">        <span class="string">&#x27;div&#x27;</span>,&#123;<span class="attr">id</span>:<span class="string">&#x27;app&#x27;</span>,<span class="attr">a</span>:<span class="number">1</span>&#125;,</span><br><span class="line">        _c(<span class="string">&#x27;span&#x27;</span>,&#123;<span class="attr">style</span>:&#123;<span class="attr">color</span>:<span class="string">&#x27;red&#x27;</span>&#125;&#125;,</span><br><span class="line">            _s(name),</span><br><span class="line">            _c(a,&#123;&#125;,_v(<span class="string">&#x27;hello&#x27;</span>))</span><br><span class="line">          )</span><br><span class="line">        )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="解析"><a href="#解析" class="headerlink" title="解析"></a>解析</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> _c 就是用来创建DOM元素的</span><br><span class="line"><span class="number">2.</span> _v 是用来创建 文本元素的</span><br><span class="line"><span class="number">3.</span> _s 是用来创建 你&#123;&#123;produc&#125;&#125;这中元素的</span><br><span class="line"><span class="number">4.</span> 基本规则就是 我们用_c函数 去创建元素 第一个参数 是你要创建的元素 第二个参数 就是你的属性集合 第二个参数至最后 就是你的孩子列表</span><br><span class="line"><span class="number">5.</span> 所以这里 如果你的孩子其中有Dom元素 我们就在用 _c 函数去创建</span><br><span class="line"><span class="number">6.</span> 以上都创建好了 我们会生成一个 函数的字符串代码</span><br><span class="line"><span class="number">7.</span> 最后在把这些函数的字符串代码 编程函数执行 去生成我们的虚拟DOM</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 以下是我们的创建的ast语法树</span></span><br><span class="line">attrs: (<span class="number">3</span>) [&#123;…&#125;, &#123;…&#125;, &#123;…&#125;]</span><br><span class="line">children: (<span class="number">3</span>) [&#123;…&#125;, &#123;…&#125;, &#123;…&#125;]</span><br><span class="line">parent: <span class="literal">null</span></span><br><span class="line">tag: <span class="string">&quot;div&quot;</span></span><br><span class="line">type: <span class="number">1</span></span><br><span class="line"><span class="comment">// 然后我们就要把这个语法书 变成我们上面的中 字符串拼接的函数</span></span><br></pre></td></tr></table></figure>
<h2 id="generate方法"><a href="#generate方法" class="headerlink" title="generate方法"></a>generate方法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 主要是把我们的AST语法树 生成我们的上面的那种 函数字符串的代码</span><br><span class="line"><span class="number">2.</span> 其核心思想 还是要做字符串做拼接</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">generate</span>(<span class="params">el</span>)</span>&#123;</span><br><span class="line">   <span class="comment">//3. 这里面有孩子有可能是还是标签元素 所以我们要调用_c函数重复上面的操作 如果是文本 我就要调用_v函数</span></span><br><span class="line">   <span class="comment">//3.1 最后 需要把我们所生成的孩子 用,连接 放在_c函数的 第二个参数后面 </span></span><br><span class="line">   <span class="keyword">let</span> children = genChildren(el.children)</span><br><span class="line">   <span class="comment">//1. 这里我们第一次肯定是从根开始 那么以根肯定是个元素 所以我们就调用 写歌函数_c的字符串代码</span></span><br><span class="line">   <span class="comment">// 把我们的标签名传进去 </span></span><br><span class="line">   <span class="keyword">let</span> code = <span class="string">`_c(&#x27;<span class="subst">$&#123;el.tag&#125;</span>&#x27;,</span></span><br><span class="line"><span class="string">	 //2. 然后_c函数的第二个参数 就是我们的属性 我们AST语法树传进来的属性 attrs是数组 我们要把他便利成</span></span><br><span class="line"><span class="string">	 // &#123;id=&quot;app&quot;,a=&#x27;1&#x27;,style=&#123;color:&#x27;red&#x27;&#125;&#125;这种格式 所以我们单写一个函数genProps 用来生成以上的格式</span></span><br><span class="line"><span class="string">   <span class="subst">$&#123;el.attrs.length?genProps(el.attrs):<span class="string">&#x27;undefined&#x27;</span>&#125;</span></span></span><br><span class="line"><span class="string">   <span class="subst">$&#123;children?(<span class="string">&#x27;,&#x27;</span>+children):<span class="string">&#x27;&#x27;</span>&#125;</span> </span></span><br><span class="line"><span class="string">   )`</span></span><br><span class="line">   <span class="keyword">return</span> code</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="genProps方法"><a href="#genProps方法" class="headerlink" title="genProps方法"></a>genProps方法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 格式化我们的属性的方法 因为我们属性 attrs 是个数组 里面每一个元素 都是我们需要把他变成我们需要的格式</span><br><span class="line"><span class="number">2.</span> attrs 是以下格式</span><br><span class="line">&#123;<span class="attr">name</span>: <span class="string">&quot;id&quot;</span>, <span class="attr">value</span>: <span class="string">&quot;app&quot;</span>&#125;</span><br><span class="line">&#123;<span class="attr">name</span>: <span class="string">&quot;class&quot;</span>, <span class="attr">value</span>: <span class="string">&quot;init&quot;</span>&#125;</span><br><span class="line">&#123;<span class="attr">name</span>: <span class="string">&quot;style&quot;</span>, <span class="attr">value</span>: &#123;…&#125;&#125;</span><br><span class="line"><span class="number">3.</span> 我们要生成的格式 &#123;<span class="attr">id</span>:<span class="string">&#x27;app&#x27;</span>,<span class="attr">class</span>:<span class="string">&#x27;init&#x27;</span>,<span class="attr">style</span>:&#123;<span class="attr">color</span>:<span class="string">&#x27;red&#x27;</span>&#125;&#125; 这种放入进我们的_c函数的第二个参数</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">genProps</span>(<span class="params">attrs</span>)</span>&#123;</span><br><span class="line"> 		<span class="comment">//1. 这里分为两种情况 第一种就是我们普通的情况 第二种就是 style:&#123;color:&#x27;red&#x27;&#125;这种 所以我们要单独判断</span></span><br><span class="line">    <span class="keyword">let</span> str = <span class="string">&#x27;&#x27;</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">let</span> i =<span class="number">0</span>; i &lt; attrs.length; i++)&#123;</span><br><span class="line">        <span class="keyword">let</span> attr = attrs[i]</span><br><span class="line">        <span class="keyword">if</span>(attr.name === <span class="string">&#x27;style&#x27;</span>)&#123;</span><br><span class="line">            <span class="comment">//2 如果属性是 style 我们要处理成 style=&#123;color:red;height:&#x27;200px&#x27;;&#125;</span></span><br><span class="line">            <span class="keyword">let</span> obj = &#123;&#125;</span><br><span class="line">            attr.value.split(<span class="string">&#x27;;&#x27;</span>).forEach(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">                <span class="comment">//分割完的每项是  color:red   height:&#x27;200px&#x27;</span></span><br><span class="line">                <span class="comment">//然后每项在分割 个我们的obj对象添加</span></span><br><span class="line">                <span class="keyword">let</span>[key,value] = item.split(<span class="string">&#x27;:&#x27;</span>)</span><br><span class="line">                obj[key] = value</span><br><span class="line">            &#125;)</span><br><span class="line">            attr.value = obj</span><br><span class="line">        &#125;</span><br><span class="line">            <span class="comment">//3.这里需要注意 我们最终出来属性 是 &#123;a:&#x27;1&#x27;,b:&#x27;zs&#x27;&#125; value应该是字符串 所以我们要用JSON.stringify转换一下</span></span><br><span class="line">            <span class="comment">// 由于每个属性之间 是逗号分割的 所以这里我们也要拼接一下逗号</span></span><br><span class="line">            str += <span class="string">`<span class="subst">$&#123;attr.name&#125;</span>:<span class="subst">$&#123;<span class="built_in">JSON</span>.stringify(attr.value)&#125;</span>,`</span></span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// console.log(str,&#x27;---str&#x27;)  这里拼接完毕之后 最外层是不带&#123;&#125;</span></span><br><span class="line">    <span class="comment">//最后把逗号截一下</span></span><br><span class="line">    <span class="keyword">return</span> <span class="string">`&#123;<span class="subst">$&#123;str.slice(<span class="number">0</span>,-<span class="number">1</span>)&#125;</span>&#125;`</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="genChildren方法"><a href="#genChildren方法" class="headerlink" title="genChildren方法"></a>genChildren方法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 就是看看你的孩子 里面 是元素还是文本 </span><br><span class="line"><span class="number">2.</span> 孩子的数据格式</span><br><span class="line">&#123;<span class="attr">tag</span>: <span class="string">&quot;div&quot;</span>, <span class="attr">type</span>: <span class="number">1</span>, <span class="attr">children</span>: <span class="built_in">Array</span>(<span class="number">1</span>), <span class="attr">parent</span>: &#123;…&#125;, <span class="attr">attrs</span>: <span class="built_in">Array</span>(<span class="number">1</span>)&#125;</span><br><span class="line">&#123;<span class="attr">tag</span>: <span class="string">&quot;span&quot;</span>, <span class="attr">type</span>: <span class="number">1</span>, <span class="attr">children</span>: <span class="built_in">Array</span>(<span class="number">1</span>), <span class="attr">parent</span>: &#123;…&#125;, <span class="attr">attrs</span>: <span class="built_in">Array</span>(<span class="number">0</span>)&#125;</span><br><span class="line">&#123;<span class="attr">type</span>: <span class="number">3</span>, <span class="attr">text</span>: <span class="string">&quot;hello&#123;&#123;product&#125;&#125;&quot;</span>&#125;</span><br><span class="line"><span class="number">3.</span> 所以我们要循环便利每一项 然后根据你不同的情况 是元素 去生成_c函数字符串 是文本 去生成 _v()函数字符串</span><br><span class="line"><span class="number">4.</span> 然后便利完毕之后 用我们的,号连接起来 放到我们_c函数的 第二个参数之后</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//5.拼接孩子看看 你有没有孩子</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">genChildren</span>(<span class="params">children</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(children,<span class="string">&#x27;--children&#x27;</span>)</span><br><span class="line">    <span class="comment">// 5.2 如果有孩子 我就要递归的去生成 判断你是不是元素 或者 文本之类的</span></span><br><span class="line">    <span class="comment">// 孩子的数据格式如下</span></span><br><span class="line">    <span class="comment">// &#123;tag: &quot;div&quot;, type: 1, children: Array(1), parent: &#123;…&#125;, attrs: Array(0)&#125;</span></span><br><span class="line">    <span class="comment">// &#123;type: 3, text: &quot;hello&quot;&#125;</span></span><br><span class="line">    <span class="keyword">if</span>(children)&#123;</span><br><span class="line">        <span class="comment">// 这里开始递归处理 去调用gen函数去看看 当前的孩子是元素还是文本 最后是个数组</span></span><br><span class="line">        <span class="comment">// 然后我们用,连接起来</span></span><br><span class="line">        <span class="keyword">return</span> children.map(<span class="function"><span class="params">child</span> =&gt;</span> gen(child)).join(<span class="string">&#x27;,&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="gen函数"><a href="#gen函数" class="headerlink" title="gen函数"></a>gen函数</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 用来把你孩子的每一项判断是文本 还是 元素 然后生成_c()函数的代码字符串 还是 _v()函数的代码字符串</span><br><span class="line"><span class="number">2.</span> 如果是元素 我们就执行生成_c的代码字符串 </span><br><span class="line"><span class="number">3.</span> 如果是文本 就分几种情况 第一种就是 hello 这种 纯文本 第二个就是 带花括号的 &#123;&#123;product&#125;&#125; 第三种 就是文本和 花括号 混合的&#123;&#123;product&#125;&#125; hello</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">gen</span>(<span class="params">node</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//6.1 如果你穿进来的孩子是元素 那么就调用我们的 generate方法去处理元素</span></span><br><span class="line">    <span class="keyword">if</span>(node.type === <span class="number">1</span>)&#123;</span><br><span class="line">        <span class="keyword">return</span> generate(node)</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="comment">// 如果你的孩子是文本 我们就调用</span></span><br><span class="line">        <span class="comment">//6.2 这里面我们处理的方式 有三种情况</span></span><br><span class="line">        <span class="comment">// 第一种 hello 就是普通的文本</span></span><br><span class="line">        <span class="comment">// 第二种是 大括号 &#123;&#123;product&#125;&#125;</span></span><br><span class="line">        <span class="comment">// 第三种就是 混合文本 既有文本 又 有大括号 &#123;&#123;product&#125;&#125; hello</span></span><br><span class="line">        <span class="keyword">let</span> text = node.text <span class="comment">// 拿到了文本 根据我们上面的情况去处理</span></span><br><span class="line">        <span class="keyword">if</span>(defaultTagRE.test(text))&#123;</span><br><span class="line">            <span class="comment">// 6.3 这里就说明 你的文本里 我匹配到 &#123;&#123;&#125;&#125; 但是也有可能是混合的 &#123;&#123;aa&#125;&#125; aaa &#123;&#123;bbb&#125;&#125; cc</span></span><br><span class="line">            <span class="comment">// 所以我把匹配出来的都放在数组里 然后循环去生成函数的字符串代码</span></span><br><span class="line">            <span class="keyword">let</span> tokens = []</span><br><span class="line">            <span class="keyword">let</span> match</span><br><span class="line">            <span class="comment">//匹配到的索引</span></span><br><span class="line">            <span class="keyword">let</span> index = <span class="number">0</span></span><br><span class="line">            <span class="comment">// defaultTagRE.lastIndex 就是 hello &#123;&#123;product&#125;&#125;  所有字符 的长度 也就是这个字符一共多长</span></span><br><span class="line">            <span class="comment">// 因为我们用上面用过一次正则  所以每次批完完毕之后我们要把索引在变成0</span></span><br><span class="line">            <span class="keyword">let</span> lastIndex = defaultTagRE.lastIndex = <span class="number">0</span></span><br><span class="line">            <span class="comment">//不停的把你双花括号里面的文本拿出来 我们此时结构的文本是 hello &#123;&#123;product&#125;&#125;</span></span><br><span class="line">            <span class="keyword">while</span> (match = defaultTagRE.exec(text)) &#123;</span><br><span class="line">                <span class="comment">//6.4 这里的match 是个数组  [&quot;&#123;&#123;product&#125;&#125;&quot;, &quot;product&quot;, index: 5, input: &quot;hello&#123;&#123;product&#125;&#125;&quot;, groups: undefined]</span></span><br><span class="line">                <span class="comment">// 在这里我们要做函数字符串的代码拼接 _v(_s(name) + &#x27;a&#x27; + _s(age) + &#x27;哈哈&#x27;)</span></span><br><span class="line">                <span class="comment">// match.index 就是你匹配到的那段&#123;&#123;&#125;&#125; 是从 hello &#123;&#123;product&#125;&#125; 哪个位置开始的</span></span><br><span class="line">                index = match.index</span><br><span class="line">                <span class="keyword">if</span>(index &gt; lastIndex)&#123;</span><br><span class="line">                   <span class="comment">// 6.5 我们就应该 把我们的&#123;&#123;&#125;&#125; 这段字符截出来 然后放进 tokens里</span></span><br><span class="line">                   tokens.push(<span class="built_in">JSON</span>.stringify(text.slice(lastIndex,index)))</span><br><span class="line">                &#125;</span><br><span class="line">                tokens.push(<span class="string">`_s(<span class="subst">$&#123;match[<span class="number">1</span>].trim()&#125;</span>)`</span>)</span><br><span class="line">                lastIndex = index + match[<span class="number">0</span>].length</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(lastIndex &lt; text.length)&#123;</span><br><span class="line">                tokens.push(<span class="built_in">JSON</span>.stringify(text.slice(lastIndex)))</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">`_v(<span class="subst">$&#123;tokens.join(<span class="string">&#x27;+&#x27;</span>)&#125;</span>)`</span></span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="comment">//这里就是说明 你就是一个纯文本 我就生成拼接文本的函数字符串 因为要带引号所以用JSON处理下</span></span><br><span class="line">            <span class="keyword">return</span> <span class="string">`_v(<span class="subst">$&#123;<span class="built_in">JSON</span>.stringify(text)&#125;</span>)`</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="with语法"><a href="#with语法" class="headerlink" title="with语法"></a>with语法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 主要是能保证我们的<span class="built_in">this</span> 取值 </span><br><span class="line"><span class="number">2.</span> 因为我们上面已经写好了我们 函数字符串拼接  但是有一些花括号里的变量 我们要去我们的vm里去 也就是在我们的<span class="built_in">this</span> 上</span><br><span class="line"><span class="number">3.</span> 所以这里 我们首先第一要把我们上面的函数拼接字符串变成一个真正的可执行函数 另外 就是当我们需要去我们的vm 去取data数据 所以就需要使用<span class="keyword">with</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">aa</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  <span class="comment">// 这里面的this 是我们的bb 而with 语法 里面的函数体 就相当于去我们的this 上面去到对应的变量 </span></span><br><span class="line">  <span class="function"><span class="title">with</span>(<span class="params"><span class="built_in">this</span></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(bb)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">aa.call(&#123;<span class="attr">bb</span>:<span class="string">&#x27;111&#x27;</span>&#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 所以 我们要把我们上面的 用字符串拼接好的函数 用<span class="keyword">with</span> 语法包一下 </span><br><span class="line"><span class="number">2.</span> 这样在调用的时候 我们就直接把我们的Vue的实例传入进去</span><br><span class="line"><span class="number">3.</span> 然后 当我们字符串拼接的函数 里面有花括号变量取值的时候 直接就可以去实例上去 data里面的数据了</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">Vue.prototype.$mount = <span class="function"><span class="keyword">function</span>(<span class="params">el</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">const</span> vm = <span class="built_in">this</span></span><br><span class="line">        <span class="keyword">const</span> options = vm.$options</span><br><span class="line">        <span class="keyword">let</span> ele = <span class="built_in">document</span>.querySelector(el)</span><br><span class="line">        <span class="keyword">if</span>(!options.render)&#123;</span><br><span class="line">            <span class="keyword">let</span> template = options.template</span><br><span class="line">            <span class="keyword">if</span>(!template &amp;&amp; ele)&#123;</span><br><span class="line">                template = ele.outerHTML</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">const</span> ast = compileToFunctions(template)</span><br><span class="line">            <span class="keyword">const</span> code = generate(ast)</span><br><span class="line">            <span class="comment">// 使用with语法包一下 方便我们的 写好的字符串拼接代码里面花括号里面的变量 去 实例上去值</span></span><br><span class="line">            <span class="keyword">const</span> render = <span class="string">`with(this)&#123;return <span class="subst">$&#123;code&#125;</span>&#125;`</span></span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="render函数"><a href="#render函数" class="headerlink" title="render函数"></a>render函数</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 接下来 我们就需要以上我们下好的 字符串的函数 变成一个真正的函数 </span><br><span class="line"><span class="number">2.</span> 这里面没有使用<span class="built_in">eval</span> 因为 会把 值也直接取出来 而且 <span class="built_in">eval</span> 也不支持传惨</span><br><span class="line"><span class="number">3.</span> 所以这里我们 <span class="keyword">new</span> 一个 <span class="built_in">Function</span> 的实例  这里也会产生一个新的作用域</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">Vue.prototype.$mount = <span class="function"><span class="keyword">function</span>(<span class="params">el</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">const</span> vm = <span class="built_in">this</span></span><br><span class="line">        <span class="keyword">const</span> options = vm.$options</span><br><span class="line">        <span class="keyword">let</span> ele = <span class="built_in">document</span>.querySelector(el)</span><br><span class="line">        <span class="keyword">if</span>(!options.render)&#123;</span><br><span class="line">            <span class="keyword">let</span> template = options.template</span><br><span class="line">            <span class="keyword">if</span>(!template &amp;&amp; ele)&#123;</span><br><span class="line">                template = ele.outerHTML</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">const</span> ast = compileToFunctions(template)</span><br><span class="line">            <span class="keyword">const</span> code = generate(ast)</span><br><span class="line">            <span class="comment">//2021 2.15 - 01 使用with语法包一下我们上面拼接好的字符串函数 方便我们里面的变量去实例上取值</span></span><br><span class="line">            <span class="keyword">const</span> render = <span class="string">`with(this)&#123;return <span class="subst">$&#123;code&#125;</span>&#125;`</span></span><br><span class="line">            <span class="comment">// 2021 2.15 - 02 然后我们new 一个Function 把我们写好的字符串函数变成一个真正可以执行的函数</span></span><br><span class="line">            <span class="keyword">let</span> fn = <span class="keyword">new</span> <span class="built_in">Function</span>(render)</span><br><span class="line">            <span class="keyword">return</span> fn</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 以上就是把我们的 AST语法树的数据 变成我们的函数字符串 </span><br><span class="line"><span class="number">2.</span> 这里无非就是字符传的拼接 根据的你参数的不同 我们去 拼接 不同的函数</span><br><span class="line"><span class="number">3.</span> 比如 元素对应_c函数 文本 对应_v函数 表达式对应_s函数</span><br><span class="line"><span class="number">4.</span> 最后我们把拼接好的函数字符串 变成真正可以调用的函数</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__title"><span class="post-copyright-info"><h>Vue源码开发-Render函数03</h></span></div><div class="post-copyright__type"><span class="post-copyright-info"><a href="http://example.com/2021/01/03/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8BRender%E5%87%BD%E6%95%B003/">http://example.com/2021/01/03/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8BRender%E5%87%BD%E6%95%B003/</a></span></div><div class="post-copyright-m"><div class="post-copyright-m-info"><div class="post-copyright-a"><h>作者</h><div class="post-copyright-cc-info"><h>北城韩雨</h></div></div><div class="post-copyright-c"><h>发布于</h><div class="post-copyright-cc-info"><h>2021-01-03</h></div></div><div class="post-copyright-u"><h>更新于</h><div class="post-copyright-cc-info"><h>2021-01-03</h></div></div><div class="post-copyright-c"><h>许可协议</h><div class="post-copyright-cc-info"><a class="icon" rel="noopener" target="_blank" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a><a rel="noopener" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></div></div></div></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Vue/">Vue</a></div><div class="post_share"></div></div><link rel="stylesheet" href="/" media="defer" onload="this.media='all'"/><div class="post-reward"><button class="tip-button reward-button"><span class="tip-button__text">打赏</span><div class="coin-wrapper"><div class="coin"><div class="coin__middle"></div><div class="coin__back"></div><div class="coin__front"></div></div></div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/weixin.jpg" target="_blank"><img class="post-qr-code-img" src="/img/weixin.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/taobao.jpg" target="_blank"><img class="post-qr-code-img" src="/img/taobao.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></button></div><script defer="defer" src="/"></script><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/01/04/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E8%99%9A%E6%8B%9FDOM04/"><img class="prev-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Vue源码开发-虚拟DOM04</div></div></a></div><div class="next-post pull-right"><a href="/2021/01/02/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%A8%A1%E6%9D%BF%E8%A7%A3%E6%9E%9002/"><img class="next-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Vue源码开发-模版解析02</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin"><img class="cover" src="/img/gin-vue-admin.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-04-16</div><div class="title">Gin-Vue-Admin</div></div></a></div><div><a href="/2021/01/08/Vue源码开发之组件的渲染10/" title="Vue源码开发-组件的渲染10"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530644&t=10679b5732c52e39eeb588040240a790" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-08</div><div class="title">Vue源码开发-组件的渲染10</div></div></a></div><div><a href="/2021/01/07/Vue源码开发之组件的合并策略09/" title="Vue源码开发-组件合并策略09"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530702&t=80943e12d5239f63beafd6d1c540855f" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-07</div><div class="title">Vue源码开发-组件合并策略09</div></div></a></div><div><a href="/2021/01/06/Vue源码开发之数组的依赖收集08/" title="Vue源码开发-数组的依赖收集08"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530644&t=10679b5732c52e39eeb588040240a790" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-06</div><div class="title">Vue源码开发-数组的依赖收集08</div></div></a></div><div><a href="/2021/01/06/Vue源码开发之生命周期合并07/" title="Vue源码开发-生命周期合并07"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530659&t=bbcfe1e86f393eecbb0722db1b92e475" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-06</div><div class="title">Vue源码开发-生命周期合并07</div></div></a></div><div><a href="/2021/01/05/Vue源码开发之批量更新06/" title="Vue源码开发-批量更新06"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530644&t=10679b5732c52e39eeb588040240a790" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-05</div><div class="title">Vue源码开发-批量更新06</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/my.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">北城韩雨</div><div class="author-info__description">一个前端界的小学生</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/wazer1987"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="/wangzhengvslike@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content"><p>感谢访问本站，如喜欢请收藏。本站主要分享前端知识，立志成为资深前端工程师，但目前是一个前端界的小学生 若喜欢可以打赏请博主喝一杯冰阔落</p> <p>另外请大家多多支持淼哥的开源项目</p> <p>https://github.com/flipped-aurora/gin-vue-admin</p> <p>🌟🌟欢迎大家start 🌟🌟</p> <p>欢迎加入博主的前端技术交流群</p> <p style="background:rgba(255,255,255,1);margin:0"> <img src="/img/code.png"></img> </p></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91-%E6%A8%A1%E7%89%88%E8%A7%A3%E6%9E%90"><span class="toc-text">Vue源码开发-模版解析</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A6%82%E4%BD%95%E5%8F%98%E6%88%90%E6%88%91%E4%BB%AC%E7%9A%84render%E5%87%BD%E6%95%B0"><span class="toc-text">如何变成我们的render函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%A7%A3%E6%9E%90"><span class="toc-text">解析</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#generate%E6%96%B9%E6%B3%95"><span class="toc-text">generate方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#genProps%E6%96%B9%E6%B3%95"><span class="toc-text">genProps方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#genChildren%E6%96%B9%E6%B3%95"><span class="toc-text">genChildren方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#gen%E5%87%BD%E6%95%B0"><span class="toc-text">gen函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#with%E8%AF%AD%E6%B3%95"><span class="toc-text">with语法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#render%E5%87%BD%E6%95%B0"><span class="toc-text">render函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-text">总结</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin"><img src="/img/gin-vue-admin.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Gin-Vue-Admin"/></a><div class="content"><a class="title" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin">Gin-Vue-Admin</a><time datetime="2021-04-15T16:00:00.000Z" title="发表于 2021-04-16 00:00:00">2021-04-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件的渲染10"/></a><div class="content"><a class="title" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10">Vue源码开发-组件的渲染10</a><time datetime="2021-01-07T16:00:00.000Z" title="发表于 2021-01-08 00:00:00">2021-01-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件合并策略09"/></a><div class="content"><a class="title" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09">Vue源码开发-组件合并策略09</a><time datetime="2021-01-06T16:00:00.000Z" title="发表于 2021-01-07 00:00:00">2021-01-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-数组的依赖收集08"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08">Vue源码开发-数组的依赖收集08</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-生命周期合并07"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07">Vue源码开发-生命周期合并07</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright"><span>&copy;2017 - 2021</span><svg style="width:2em; height:2em;position: relative;top: 10px;" aria-hidden="true"><use xlink:href="#icon-Butterfly"></use></svg><span>北城韩雨</span></div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><p><a style="margin-inline:5px" target="_blank" href="https://hexo.io/"><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为Hexo"></a><a style="margin-inline:5px" target="_blank" href="https://butterfly.js.org/"><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用butterfly"></a><a style="margin-inline:5px" target="_blank" href="https://www.jsdelivr.com/"><img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用JsDelivr为静态资源提供CDN加速"></a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/medium-zoom/dist/medium-zoom.min.js"></script><div class="js-pjax"></div><script defer src="/live2d-widget/autoload.js"></script><script src="/js/coin.js"></script><script async src="//at.alicdn.com/t/font_2700632_3rtvhzzpl3h.js"></script><script src="/js/liuxing.js" data-pjax></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0ms');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://cdn.jsdelivr.net/gh/graingert/wow@1.3.0/dist/wow.min.js"></script><script defer src="https://cdn.jsdelivr.net/npm/hexo-butterfly-wowjs/lib/wow_init.min.js"></script><script async src="//at.alicdn.com/t/font_2032782_8d5kxvn09md.js"></script><!-- hexo injector body_end end --></body></html>